/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package axil.engine.access.discovery;

import axil.api.Context;
import axil.api.access.Accessible;
import axil.api.access.AccessibleType;
import axil.api.access.Accessor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * The abstract base class for a variety of host application object
 * introspection mechanisms.
 */
public abstract class Discovery {
	private static final Map<Class, Discovery> types =
	    new ConcurrentHashMap<Class, Discovery>();


	public static Discovery matching(Object object) {
		final Class c = object.getClass();
		Discovery type = types.get(c);
		if (type == null) {
			AccessibleType v = (AccessibleType)c.getAnnotation(AccessibleType.class);
			if (v == null) {
				if (object instanceof Accessible) {
					type = new ProgrammaticDiscovery();
				}
			} else {
				if (object instanceof Accessible) {
					type = new HybridDiscovery(c, v);
				} else {
					type = new ReflectiveDiscovery(c, v);
				}
			}
			if (type != null) {
				types.put(c, type);
			}
		}
		return type;
	}


	/**
	 * Get an accessor for the given field residing within the parent object.
	 *
	 * @param context
	 * 	The context being used for the access.
	 *
	 * @param parent
	 * 	The parent object. This is the object that is expected to contain the
	 * 	given field. The parent object is never null.
	 *
	 * @param field
	 * 	The name of the field as it appears in the Axil script. Note that Axil
	 * 	supports a highly flexible identifier mechanism, allowing spaces and
	 * 	punctuation in identifiers. It is the responsibility of the host
	 * 	application to define how identifiers from Axil are mapped to actual
	 * 	object fields.
	 *
	 * @return
	 * 	Returns a non-null accessor object. If the field cannot be found, then
	 * 	an exception is thrown.
	 */
	public abstract Accessor accessor(Context context, Object parent,
	                                  String field);
}
